package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.service.IPrjCstAccInfoService;
import cn.fl.project.vo.PrjCstAccInfoVO;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 项目基础信息表 控制层 华润银行
 */
@Controller
@RequestMapping("/institution/hrbank")
public class PrjBscInfoInsHrbankController {

    private static final Logger logger = LoggerFactory.getLogger(PrjBscInfoInsHrbankController.class);

    @Autowired
    IPrjCstAccInfoService prjCstAccInfoService;//项目客户基础信息表

    /**
     * 保存 项目客户信息
     */
    @RequestMapping(value = "/hrbankSavePrjAccInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO hrbankSavePrjAccInfo(HttpServletRequest request) throws BaseException {
        StopWatch stopWatch = new StopWatch("华润银行信息采集保存接口计时统计");
        stopWatch.start();
        BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "修改项目客户账户信息及校验入参"+ JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName());
        String cstAccInfoId = request.getParameter("cstAccInfoId");//项目客户信息表id
        String insCode = request.getParameter("insCode");//机构code
        String prjId = request.getParameter("prjId");//项目id
        String cstId = request.getParameter("cstId");//客户id
        String cstNm = request.getParameter("cstNm");//客户姓名
        String bkNm = request.getParameter("bkNm");//银行名称
        String bkCd = request.getParameter("bkCd");//银行代码
        String accNo = request.getParameter("accNo");//银行账号
        String mp = request.getParameter("mp");//手机号
        String certNo = request.getParameter("certNo");//身份证
        DataResultVO dataResultVO = null;
        try {
            if (StringUtils.isEmpty(prjId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目id为空!");
            }
            if (StringUtils.isEmpty(cstId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户id为空!");
            }
            if (StringUtils.isEmpty(cstNm)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户名称为空!");
            }
            if(StringUtils.isEmpty(bkNm) || StringUtils.isEmpty(bkCd)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护开户银行信息!");
            }
            if(StringUtils.isEmpty(accNo)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行卡号!");
            }
            if(StringUtils.isEmpty(mp)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行预留手机号!");
            }
            PrjCstAccInfoQueryCondition prjCstAccInfoQC = new PrjCstAccInfoQueryCondition();
            prjCstAccInfoQC.setPrjId(Long.valueOf(prjId));
            prjCstAccInfoQC.setIsDel(BaseConstant.IS_YESNO_NO);
            prjCstAccInfoQC.setInsCode(insCode);
            BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "根据prjId获取账户信息prjId" + prjId);
            List<PrjCstAccInfoVO> prjCstAccInfoVOS = this.prjCstAccInfoService.searchList(prjCstAccInfoQC);
            BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "根据prjId获取账户信息prjCstAccInfoVOS" + JSON.toJSONString(prjCstAccInfoVOS));
            PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
            PrjCstAccInfoVO prjCstAccInfoVO = null;
            if (CollectionUtils.isNotEmpty(prjCstAccInfoVOS)) {
                prjCstAccInfoVO = prjCstAccInfoVOS.get(0);
                //说明该项目已存在账户信息 则跟据id进行修改
                condition.setId(prjCstAccInfoVO.getId());
            }
            condition.setPrjId(Long.parseLong(prjId));
            condition.setCstId(Long.parseLong(cstId));
            condition.setInsCode(insCode);
            condition.setCstNm(cstNm);
            condition.setAccNm(cstNm);
            condition.setBkCd(bkCd);
            condition.setBkNm(bkNm);
            condition.setAccNo(accNo.trim());
            condition.setMp(mp);
            condition.setCertNo(certNo.trim());
            condition.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
            dataResultVO = this.prjCstAccInfoService.modifyPrjAccInfo(condition);
            BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "修改项目客户账户信息表结果dataResultVO=" + JSON.toJSONString(dataResultVO));
            if(Objects.equals(BaseConstant.IS_YESNO_YES, dataResultVO.getSuccess())){
                Map<String,String> resultMap = (Map<String, String>) dataResultVO.getData();
                String chkRstCd;
                String rstDesc;
                chkRstCd = resultMap.get("chkRstCd");
                rstDesc = resultMap.get("rstDesc");
                if (chkRstCd.equals("1")) {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                    dataResultVO.setInfo("四要素信息校验通过");
                } else {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo(rstDesc);
                }
            }
        } catch (BaseException ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "新增或修改项目客户账户信息及校验,prjId:" + prjId + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception e) {
            BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "保存失败，系统异常",CurrentThreadContext.getCurrentUserName(), e);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }
        stopWatch.stop();
        BaseLogUtils.info(logger, "hrbankSavePrjAccInfo", "新增或修改项目客户账户信息及校验返回dataResultVO=" +
                JSON.toJSONString(dataResultVO) + "-耗时=" + stopWatch.prettyPrint(), CurrentThreadContext.getCurrentUserName());
        return dataResultVO;
    }
}
